package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.DynamicConfigLab;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.DateUtil;
import java.util.Date;

public abstract class TravelReimburseListDetailFragment extends Fragment
    implements OnClickListener {
  //wyn
  private static final int START_DATE = 0;// 出发时间
  private static final int END_DATE = 1;// 结束时间
  protected static final int TRAFTOOL = 2;// 交通工具
  protected BaseVolleyActivity mActivity;
  protected CurrentUser mCurrentUser;
  protected String mId;
  protected String mName;//交通工具
  protected TextView etstartDate;
  protected EditText etstartEar;
  protected TextView etendDate;
  protected EditText etendEar;
  protected TextView tvtraftool;
  protected EditText ettraftoolCost;
  protected EditText etcityCost;
  protected EditText ettravelDays;//差旅天数
  protected EditText etstandard;//补助标准
  protected TextView tvallStandard;//补助合计
  protected EditText etnbuyCost;
  protected EditText etliveCost;
  protected EditText etotherCost;
  protected EditText etPages;
  protected TextView tvbillFee;//票据费用
  protected EditText etcertificateNo;
  protected EditText etNote;
  protected TextView tvallCost;
  protected ImageView ibstartDate;
  protected ImageView ibendDate;
  protected ImageView ibtraftool;
  protected TextView tvInvestigateContent;//审查修改附言

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle("差旅明细");
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mActivity = (BaseVolleyActivity) getActivity();
    mCurrentUser = CurrentUser.newInstance(getActivity());
    //		setTitle();
    super.onCreate(savedInstanceState);
  }

  //	/**
  //	 * 设置标题
  //	 */
  //	protected abstract void setTitle();

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_add_travel_reimburse_list, container, false);
    etstartDate = (TextView) v.findViewById(R.id.et_startDate);
    etendDate = (TextView) v.findViewById(R.id.et_endDate);

    etstartEar = (EditText) v.findViewById(R.id.et_startEar);
    etendEar = (EditText) v.findViewById(R.id.et_endEar);
    tvtraftool = (TextView) v.findViewById(R.id.tv_traftool);
    ettraftoolCost = (EditText) v.findViewById(R.id.et_traftool_cost);
    etcityCost = (EditText) v.findViewById(R.id.et_city_cost);
    ettravelDays = (EditText) v.findViewById(R.id.et_travel_days);
    etstandard = (EditText) v.findViewById(R.id.et_standard);
    tvallStandard = (TextView) v.findViewById(R.id.tv_all_standard);

    etnbuyCost = (EditText) v.findViewById(R.id.et_nbuy_cost);
    etliveCost = (EditText) v.findViewById(R.id.et_live_cost);
    etotherCost = (EditText) v.findViewById(R.id.et_other_cost);
    etPages = (EditText) v.findViewById(R.id.et_pages);
    tvbillFee = (TextView) v.findViewById(R.id.tv_billFee);
    etcertificateNo = (EditText) v.findViewById(R.id.et_certificateNo);
    etNote = (EditText) v.findViewById(R.id.et_note);
    tvallCost = (TextView) v.findViewById(R.id.tv_all_cost);

    ibstartDate = (ImageView) v.findViewById(R.id.ib_startDate);
    ibendDate = (ImageView) v.findViewById(R.id.ib_endDate);
    ibtraftool = (ImageView) v.findViewById(R.id.ib_traftool);
    tvInvestigateContent = (TextView) v.findViewById(R.id.tv_investigate_content);
    //动态配置列   ---隐藏和显示，title
    DynamicConfigLab.newInstance(mActivity, getString(R.string.module_appFinance))
        .autoConfigTitle(v);
    DynamicConfigLab.newInstance(mActivity, getString(R.string.module_appFinance))
        .autoConfigVisiblity(v);
    ibstartDate.setOnClickListener(this);
    ibendDate.setOnClickListener(this);
    ibtraftool.setOnClickListener(this);

    ettravelDays.addTextChangedListener(watcher());//差旅天数
    etstandard.addTextChangedListener(watcher());//补助标准

    ettraftoolCost.addTextChangedListener(watcher());
    etcityCost.addTextChangedListener(watcher());
    etnbuyCost.addTextChangedListener(watcher());
    etliveCost.addTextChangedListener(watcher());
    etotherCost.addTextChangedListener(watcher());

    initData();
    return v;
  }

  protected TextWatcher watcher() {
    return new TextWatcher() {
      @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

      }

      @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
        try {
          double traftoolCost = 0;
          double cityCost = 0;
          double nbuyCost = 0;
          double liveCost = 0;
          double otherCost = 0;
          double billFee = 0.0;
          double allCost = 0.0;

          double days = 0;
          double standard = 0.0;
          double allStandard = 0.0;
          if (!TextUtils.isEmpty(ettraftoolCost.getText().toString())) {
            traftoolCost = Double.parseDouble(ettraftoolCost.getText().toString());
          }
          if (!TextUtils.isEmpty(etcityCost.getText().toString())) {
            cityCost = Double.parseDouble(etcityCost.getText().toString());
          }
          if (!TextUtils.isEmpty(etnbuyCost.getText().toString())) {
            nbuyCost = Double.parseDouble(etnbuyCost.getText().toString());
          }
          if (!TextUtils.isEmpty(etliveCost.getText().toString())) {
            liveCost = Double.parseDouble(etliveCost.getText().toString());
          }
          if (!TextUtils.isEmpty(etotherCost.getText().toString())) {
            otherCost = Double.parseDouble(etotherCost.getText().toString());
          }
          if (!TextUtils.isEmpty(ettravelDays.getText().toString())) {
            days = Double.parseDouble(ettravelDays.getText().toString());
          }
          if (!TextUtils.isEmpty(etstandard.getText().toString())) {
            standard = Double.parseDouble(etstandard.getText().toString());
          }
          //差旅补助
          allStandard = days * standard;
          //					tvallStandard.setText(allStandard+"");
          //
          tvallStandard.setText(String.valueOf(allStandard));
          //票据费用
          billFee = traftoolCost + cityCost + nbuyCost + liveCost + otherCost;
          tvbillFee.setText(billFee + "");
          //费用小计
          allCost = traftoolCost + cityCost + nbuyCost + liveCost + otherCost + allStandard;
          tvallCost.setText(allCost + "");
        } catch (NumberFormatException e) {
          e.printStackTrace();
        }
      }

      @Override public void afterTextChanged(Editable s) {

      }
    };
  }

  protected void initData() {

  }

  @Override public void onClick(View arg0) {
    switch (arg0.getId()) {
      case R.id.ib_startDate:

        showDialog(START_DATE, new Date());
        break;
      case R.id.ib_endDate:

        showDialog(END_DATE, new Date());
        break;
      // 交通工具
      case R.id.ib_traftool:

        showDialog(TRAFTOOL, new Date());
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;
    FragmentManager fm = getActivity().getSupportFragmentManager();
    switch (requestCode) {
      case START_DATE:
        dialog = WorkProgressTimeDialogFragment.newInstance(date, R.string.choose_startDate);
        break;

      case END_DATE:
        dialog = WorkProgressTimeDialogFragment.newInstance(date, R.string.choose_endDate);

        break;
      case TRAFTOOL:
        dialog = new TrafToolDialogFragment();

        break;

      default:
        break;
    }
    dialog.setTargetFragment(TravelReimburseListDetailFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == START_DATE && data != null) {
      Date chooseDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      String chooseDateString = DateUtil.dateToString(chooseDate);
      etstartDate.setText(chooseDateString);
      return;
    }
    if (requestCode == END_DATE && data != null) {
      Date chooseDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      String chooseDateString = DateUtil.dateToString(chooseDate);
      etendDate.setText(chooseDateString);
      return;
    }
    if (requestCode == TRAFTOOL && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mName = customer.getName();
      mId = customer.getId();
      tvtraftool.setText(mName);//交通工具
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  //判断是否所有都为空
  public boolean allEmpty() {

    if (TextUtils.isEmpty(etstartDate.getText().toString())
        && TextUtils.isEmpty(etstartEar.getText().toString())
        && TextUtils.isEmpty(etendDate.getText().toString())
        && TextUtils.isEmpty(etendEar.getText().toString())
        && TextUtils.isEmpty(tvtraftool.getText().toString())
        && TextUtils.isEmpty(ettraftoolCost.getText().toString())
        && TextUtils.isEmpty(tvallStandard.getText().toString())
        && TextUtils.isEmpty(etcityCost.getText().toString())
        && TextUtils.isEmpty(ettravelDays.getText().toString())
        && TextUtils.isEmpty(etnbuyCost.getText().toString())
        && TextUtils.isEmpty(etliveCost.getText().toString())
        && TextUtils.isEmpty(etotherCost.getText().toString())
        && TextUtils.isEmpty(etPages.getText().toString())
        && TextUtils.isEmpty(etcertificateNo.getText().toString())
        && TextUtils.isEmpty(etNote.getText().toString())
        && TextUtils.isEmpty(tvallCost.getText().toString())
        && TextUtils.isEmpty(tvallStandard.getText().toString())
        && TextUtils.isEmpty(tvbillFee.getText().toString())) {
      return true;
    } else {
      return false;
    }
  }
}
